package com.tuda.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.ValueFilter;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 实体类转化
 * Created by lv on 2019/3/19 13:22.
 */
public class EntityConvertUtils {

    //属性值过滤
    private final static ValueFilter valueFilter = new ValueFilter() {
        @Override
        public Object process(Object source, String name, Object value) {
            return value;
        }
    };

    /**
     * 将实体转换为A to B
     *
     * @param A
     * @param clazz
     * @return
     */
    public static <T> T convertAToB(Object A, Class<T> clazz) {
        if (A == null || clazz == null) {
            return null;
        }
        String json = JSONObject.toJSONString(A, valueFilter);
        return JSONObject.parseObject(json, clazz);
    }

    public static List<String> str2list(String str, String... strs) {
        ArrayList<String> list = new ArrayList();
        if (ToolUtil.isNotEmpty(str)) {
            list.add(str);
        } else {
            if (ToolUtil.isEmpty(strs)) {
                return new ArrayList<>();
            }
            List<String> lists = Arrays.asList(strs);
            for (String string : lists) {
                list.add(string);
            }
        }
        if (ToolUtil.isNotEmpty(list)) {
            return list;
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * 将AList->BList
     *
     * @param AList
     * @param clazz
     * @return
     */
    public static <T> List<T> convertAListToBList(List<?> AList, Class<T> clazz) {
        if (AList == null || AList.size() <= 0) {
            return new ArrayList<T>();
        }
        List<T> list = new ArrayList<T>();
        for (Object A : AList) {
            list.add(convertAToB(A, clazz));
        }
        return list;
    }


    public static <T> T setNullValue(T source) {
        try {
            Field[] fields = source.getClass().getDeclaredFields();
            for (Field field : fields) {
                if (field.getGenericType().toString().equals(
                        "class java.lang.String")) {
                    field.setAccessible(true);
                    Object obj = field.get(source);
                    if (obj != null && obj.equals("")) {

                        field.set(source, null);
                    }
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return source;
    }

    public static String listToString(List<String> list) {
        if (list == null) {
            return null;
        }
        StringBuilder result = new StringBuilder();
        boolean first = true;
        //第一个前面不拼接","
        for (String string : list) {
            if (first) {
                first = false;
            } else {
                result.append(",");
            }
            result.append(string);
        }
        return result.toString();
    }

    public static String handlePhone(String phone) {
        try {
            return phone.substring(0, 3) + "****" + phone.substring(7, 11);
        } catch (Exception e) {
            return phone;
        }
    }
}
